// moonshotApiBrowser.js

import axios from 'axios';
const baseURL = "https://api.moonshot.cn/v1";
const apiKey = "rYQkXPihRmZwDjrAHLvEoSUi";

let history = [{
    role: "system",
    content: "你是 Kimi，由 Moonshot AI 提供的人工智能助手，你更擅长中文和英文的对话。你会为用户提供安全，有帮助，准确的回答。同时，你会拒绝一切涉及恐怖主义，种族歧视，黄色暴力等问题的回答。Moonshot AI 为专有名词，不可翻译成其他语言。"
}];

async function chat(prompt) {
    try {
        history.push({ role: "user", content: prompt });

        const response = await axios.post(`${baseURL}`, {
            model: "moonshot-v1-8k",
            messages: history,
        }, {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${apiKey}`
            }
        });

        history.push(response.data.choices[0].message);

        return response.data.choices[0].message.content;
    } catch (error) {
        console.error("Failed to send message:", error);
        throw new Error("Failed to chat with Moonshot AI.");
    }
}

export default {
    chat
};
